<template>
	<view>
		<view v-if="curProcessConfig === null" class="flcc pad3 c12 bdda mar3">
			工序流转已结束，等待下段工序关联
		</view>
		<view v-else>
			<view  class="flrc bdbda marlr1">
				<view class="list-style-2">报工工序：</view>
				<view class="flex1 tar fwb pad2" v-if="$com.isbool(curProcessConfig.process)">
					{{curProcessConfig.process.name}}
				</view>
			</view>
			<view class="flrc padtb1">
				<view class=" padlr2">成品</view>
				<view class="flex1" :key="numUpdateKey">
					<!-- 第一工序才允许编辑 -->
					<input v-if="pd.paperSelected.length == 0" type="digit" v-model.number="pd.num" class="tac lh2 fs18 bdso" placeholder-class="c12 fs12" />
					<!-- 有来料的时候不允许编辑 -->
					<input :key="pd.numSum" v-if="pd.paperSelected.length > 0" v-model.number="pd.num" class="tac bc13 lh2 fs18" disabled placeholder-class="c12 fs12" />
				</view>
				<view class=" padlr2">废品</view>
				<view class="flex1">
					<input @blur="rebuildNums()" adjust-position type="digit" v-model.number="pd.numF" 
					:disabled="curProcessConfig.isNeedCheck"
					class="tac bcf lh2 fs18 bdso" placeholder-class="c12 fs12" />
				</view>
			</view>
			<view class="marlr1 mart3" :key="selectedPaperKey">
				<view class=" bdso round2 pos-r ofh padl2 mart2" v-for="paper,index in pd.paperSelected"
				:key="index + '_' + paper.key">
					<view class="bcf flrc bdbso" @tap="mySlide(index)">
						<view class="flex1">{{paper.sn}}</view>
						<view class="c12">#{{paper.id}}</view>
					</view>
					<view class="flrc bc13 flex-fill">
						<view class="flex2" @tap="mySlide(index)" v-if="paper.process">{{paper.process.name}}</view>
						<view class="flex2" v-else>工序丢失</view>
						<view class="flex2 tac">
							<text>{{paper.num}}</text>
							<text class="padlr1">/</text>
							<text class="c12">{{paper.numUsed}}</text></view>
						<view class="cu-btn pos-r c05" @tap="redNumUse(index,paper)">
							<ripple></ripple>
							<view class="fa fa-minus-circle"></view>
						</view>
						<view class="flex1 bcf">
							<input adjust-position @blur="checkNumInput(index,paper)" @input="checkNumInput(index,paper)"
							selection-start="0" selection-end="10" type="number"
							:disabled="curProcessConfig === null" class="tac lh2 fs18" placeholder-class="c12 fs12"
							v-model.number="paper.numUse" />
						</view>
						<view class="cu-btn pos-r c05" @tap="addNumUse(index,paper)">
							<ripple></ripple>
							<view class="fa fa-plus-circle"></view>
						</view>
					</view>
					<sslide v-if="pd.canAddSrcPapers" ref="mySlideBox" class="bg-orange pad2 w40 flcc">
						<view @tap="deleteSrcPaper(index)" class="bcf05 c13 pad2 tac flcc c11 op9 w100">
							删除
						</view>
					</sslide>
				</view>
				<view class="flrc bg-gradual-blue padlr2" v-if="pd.paperSelected.length > 1">
					<view>合计：</view>
					<view class="flex3">{{pd.numTotal}}</view>
					<view>使用：</view>
					<view class="flex3" :key="numUpdateKey">
						<input adjust-position @blur="autoFillNumByUse" @input="autoFillNumByUse"
						selection-start="0" selection-end="10" type="number"
						:disabled="curProcessConfig === null" class="tac lh2 fs18 bdbso" placeholder-class="c12 fs12"
						v-model.number="pd.numUse" />
					</view>
					<view class="pos-r pad3 flex1 flrc" @tap="useAll">
						<view class="fa fa-level-down fa-rotate-90"></view>
						<ripple></ripple>
					</view>
				</view>
			</view>
			<template v-if="$com.isbool(pd.curProcessConfig) && $com.isbool(pd.curProcessConfig.params) ">
				<view class="mart3" v-if="pd.curProcessConfig">
					<view class="list-style-2">生产数据</view>
					<view class="flrc lh2 pad1 bdbda" v-for="(item,index) in pd.curProcessConfig.params.fields">
						<view class="flex1">{{item.name}}</view>
						<input adjust-position placeholder-class="c12 fs12" class="flex5 tac bc13 fs18" :placeholder="`请输入${item.name}`" v-model="item.value" />
					</view>
				</view>
			</template>
		</view>

	</view>
</template>

<script>
import log from '../../../assets/js/log';
import PublicData from './PublicData'
	export default {
		props:{
			flow:0,
		},
		data() {
			return {
				pd:PublicData,
				lastProcess:null,
				lastProcessId:0,
				lastProcessCount:0,
				curProcessConfig:null,
				numUpdateKey:"",
				selectedPaperKey:"",
			}
		},
		methods: {
			rebuildNums(){
				this.pd.rebuildNums()
				this.numUpdateKey = Math.random()
			},
			redNumUse(index,paper){
				paper.numUse--
				this.$set(this.pd.paperSelected,index,paper)
				this.checkNumInput(index,paper)
			},
			addNumUse(index,paper){
				paper.numUse++
				this.$set(this.pd.paperSelected,index,paper)
				this.checkNumInput(index,paper)
			},
			checkNumInput(index,paper){
				if (paper.num - paper.numUsed < paper.numUse){
					this.$com.showToast("数量超出限制，已自动修改")
					paper.numUse = paper.num - paper.numUsed
					paper.key = Math.random()
					this.$set(this.pd.paperSelected,index,paper)
				}
				this.pd.rebuildNums()
			},
			autoChooseProcess(){
				this.curProcessConfig = this.pd.autoChooseProcess()
			},
			autoFillNumByUse(){
				this.pd.autoFillNumByUse()
			},
			useAll(){
				this.pd.numUse = this.pd.numTotal*1
				this.pd.autoFillNumByUse()
				this.numUpdateKey = Math.random()
			},
			deleteSrcPaper(index){
				this.pd.deleteSrcPaper(index)
				//this.selectedPaperKey = Math.random()
				this.pd.rebuildNums()
			}
		},
		watch:{
			'pd.prepareCount'(v){
				if (v === 3){
					this.autoChooseProcess()
				}
			}
		}
	}
</script>

<style>

</style>
